
import pandas as pd
import numpy as np

def calc_corrs(se1, se2, tag):
  c1 = se1.corr(se2, method='pearson')
  c2 = se1.corr(se2, method='spearman')
  c3 = se1.corr(se2, method='kendall')
  print(tag + ", c1:" + str(c1) + ", c2:" + str(c2) + ", c3:" + str(c3))
  return c1,c2,c3

def card_cal(prob, base_point=50, odds=1/19, pdo=30, keep=3):
  if prob < 0:
    return prob
    
  B = pdo / np.log(2)
  A = base_point + B * np.log(odds)
  y = np.log(prob / (1-prob))
  score = round(A-B*y, keep)
  
  s1 = score
  s1 = round(s1)
  if s1<0:
    s1 = 0
  if s1>100:
    s1 = 100
  return s1


'''
pdSc = pd.read_excel("./w12_y2_提交预估分转分.xlsx")
print(pdSc)
print(pdSc.columns.tolist())
# [122891 rows x 10 columns]
# ['y1', 'y2', 'y3', 'flag', 'id', 'mob_hash', 'certno_hash', 'apply_date', 
# 'y2_预估分_E-a_s202407_2', 'y2_转分_E-a_s202407_2']


vOrigFreq = {}
for px in pdSc['y2_转分_E-a_s202407_2'].tolist(): #'fpd15_转分_B-a'].tolist():
  vOrigFreq[px] = vOrigFreq.get(px,0) + 1
sScFreqOrig = sorted(vOrigFreq.items(), key=lambda x: x[0], reverse=False)
for kv in sScFreqOrig:
    print("orig," + str(kv[0]) + "," + str(kv[1]))


vSc = []
vTs = []
dTsFreq = {}
dTsSegFreq = {}
fOut = open("./y2_E-a+s202407_预估分转分", "w")
for idx,row in pdSc.iterrows():
  rs = float(row['y2_预估分_E-a_s202407_2'])
  
  ts = card_cal(rs, base_point=75, odds=0.07, pdo=20)*6 + 300 # y2_E-a+s202407
  
  fOut.write(str(rs) + "\t" + str(ts) + "\n")
  
  vSc.append(rs)
  vTs.append(ts)
  dTsFreq[ts] = dTsFreq.get(ts, 0) + 1
  
  tsSeg = int(ts/10)
  dTsSegFreq[tsSeg] = dTsSegFreq.get(tsSeg, 0) + 1

fOut.close()
calc_corrs(pd.Series(vSc), pd.Series(vTs), 'fpd')

sScFreq = sorted(dTsFreq.items(), key=lambda x: x[0], reverse=False)
for kv in sScFreq:
    print(str(kv[0]) + "," + str(kv[1]))
print("###############")
sScFreqS = sorted(dTsSegFreq.items(), key=lambda x: x[0], reverse=False)
for kv in sScFreqS:
    print(str(kv[0]) + "," + str(kv[1]))
'''





pdSc = pd.read_excel("./w10_fpd15_提交预估分转分.xlsx")
print(pdSc)
print(pdSc.columns.tolist())
# [100000 rows x 13 columns]
# ['fpd15_flag', 'mob3_15_flag', 'into_id', 'id', 'mob_hash', 'certno_hash', 'apply_date', 
# 'fpd15_预估分_B-a', 'fpd15_转分_B-a', 'Y_预估分_202407_2', 'Y_转分_202407_2', 
# 'fpd15_预估分_B-a-s202407_2', 'fpd15_转分_B-a-s202407_2']


vOrigFreq = {}
for px in pdSc['fpd15_转分_E-a-s202407_2'].tolist(): #'fpd15_转分_B-a'].tolist():
  vOrigFreq[px] = vOrigFreq.get(px,0) + 1
sScFreqOrig = sorted(vOrigFreq.items(), key=lambda x: x[0], reverse=False)
for kv in sScFreqOrig:
    print("orig," + str(kv[0]) + "," + str(kv[1]))
    

vSc = []
vTs = []
dTsFreq = {}
dTsSegFreq = {}
fOut = open("./fpd15_E-a+s202407_预估分转分", "w")
for idx,row in pdSc.iterrows():
#  rs = float(row['fpd15_预估分_B-a'])
#  rs = float(row['Y_预估分_202407_2'])
#  rs = float(row['fpd15_预估分_B-a-s202407_2'])
#  rs = float(row['fpd15_预估分_E-a-B-a'])
  rs = float(row['fpd15_预估分_E-a-s202407_2'])
    
#  ts = card_cal(rs, base_point=170, odds=0.1, pdo=160)*6 + 300  # B-a 
#  ts = card_cal(rs, base_point=50, odds=0.1, pdo=30)*6 + 300 # s202407
#  ts = card_cal(rs, base_point=75, odds=0.1, pdo=65)*6 + 300
#  ts = card_cal(rs, base_point=65, odds=0.1, pdo=65)*6 + 300 # fpd15: E-a+B-a
  ts = card_cal(rs, base_point=70, odds=0.1, pdo=40)*6 + 300 # fpd15: E-a+s202407_2
    
  fOut.write(str(rs) + "\t" + str(ts) + "\n")
  
  vSc.append(rs)
  vTs.append(ts)
  dTsFreq[ts] = dTsFreq.get(ts, 0) + 1
  
  tsSeg = int(ts/10)
  dTsSegFreq[tsSeg] = dTsSegFreq.get(tsSeg, 0) + 1

fOut.close()
calc_corrs(pd.Series(vSc), pd.Series(vTs), 'fpd')

sScFreq = sorted(dTsFreq.items(), key=lambda x: x[0], reverse=False)
for kv in sScFreq:
    print(str(kv[0]) + "," + str(kv[1]))
print("###############")
sScFreqS = sorted(dTsSegFreq.items(), key=lambda x: x[0], reverse=False)
for kv in sScFreqS:
    print(str(kv[0]) + "," + str(kv[1]))

  
